package com.lxu.report.controller;

import com.lxu.report.entity.YwReportData;
import com.lxu.report.service.TemplateNameAssembleService;
import com.lxu.report.service.WordTemplateService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.net.URLEncoder;

@Controller
@RequestMapping("/report")
public class ReportToolController {

    @Resource
    private WordTemplateService wordTemplateService;

    @Resource
    private TemplateNameAssembleService templateNameAssembleService;
    //http://localhost:8081/report/templateToWord?projectId=ccf9e88163624fd4b0fc67515b6de461&templateTypeCode=JDBG&id=ccf9e88163624fd4b0fc67515b6de461&templateCode=JDBG-D-1-0001
    @RequestMapping("/templateToWord")
    public void templateWord(YwReportData ywReportData, HttpServletResponse response) throws Exception {
        response.reset();// 清空输出流
        response.setHeader("content-Type", "application/msword");
        // 下载文件的默认名称
        response.setHeader("Content-Disposition", URLEncoder.encode(templateNameAssembleService.assembleTemplateName(ywReportData), "utf-8") + ".docx");
        OutputStream out = response.getOutputStream();
        //读取word模板
        wordTemplateService.exportWordTemplate(out, ywReportData);
    }
}

